<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="仓库名称" prop="storeName">
        <el-input
          v-model="queryParams.storeName"
          placeholder="请输入仓库名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <div style="display: flex;margin-right: 10px;float: left">
        <div class="addressTitle">
          所在地
        </div>
        <div style="flex: 1;">
          <treeselect v-model="queryParams.city" :options="cityOptions" :show-count="true" placeholder="请选所在地"  style="width: 200px"/>
        </div>
      </div>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['wms:store:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['wms:store:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['wms:store:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="storeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="仓库名称" align="center" prop="storeName" />
      <el-table-column label="所在地" align="center" prop="cityName" />
      <el-table-column label="详细地址" align="center" prop="address" />
      <el-table-column label="容量" align="center" prop="maxCount" />
      <el-table-column label="剩余容量" align="center" prop="syCount" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['wms:store:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['wms:store:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改仓库管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="仓库名称" prop="storeName">
          <el-input v-model="form.storeName" placeholder="请输入仓库名称" />
        </el-form-item>
        <el-form-item label="所在地" prop="city">
          <treeselect v-model="form.city" :options="cityOptions" :show-count="true" placeholder="请选所在地" />
        </el-form-item>
        <el-form-item label="已用库存" v-if="form.id">
          <el-input v-model="form.yyMaxCount" disabled/>
        </el-form-item>
        <el-form-item label="容量" prop="maxCount">
          <el-input v-model="form.maxCount" placeholder="请输入容量" />
        </el-form-item>
        <el-form-item label="详细地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入详细地址" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" v-loading="btnLoading">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listStore, getStore, delStore, addStore, updateStore } from "@/api/wms/store";
import { listCityTree } from "@/api/wms/city";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Store",
  components: {Treeselect},
  data() {
    return {
      //按钮加载
      btnLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      rows: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 仓库管理表格数据
      storeList: [],
      cityOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        storeName: null,
        city: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        storeName: [
          { required: true, message: "仓库名称不能为空", trigger: "blur" },
          { max: 50, message: '仓库名称长度最多 50个字', trigger: 'blur' }
        ],
        maxCount: [
          { required: true, message: "容量不能为空", trigger: "blur" },
          { pattern: /^(0|[1-9])\d{0,8}$/, message: '容量只能是整数最大11位数', trigger: 'blur' }
        ],
        address: [
          { max: 100, message: '详细地址长度最多 100个字', trigger: 'blur' }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getCity();
  },
  methods: {
    /** 查询仓库管理列表 */
    getList() {
      this.loading = true;
      listStore(this.queryParams).then(response => {
        this.storeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getCity(){
      listCityTree().then(response => {
        this.cityOptions = response.data;
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        storeName: null,
        city: null,
        maxCount: null,
        address: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.rows = selection
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加仓库管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getStore(id).then(response => {
        this.form = response.data;
        this.form.oldMaxCount = this.form.maxCount;
        this.form.yyMaxCount = this.form.maxCount - this.form.syCount;
        this.open = true;
        this.title = "修改仓库管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.btnLoading = true
          if (this.form.id != null) {
            updateStore(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(f => {
              this.btnLoading = false
            });
          } else {
            addStore(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(f => {
              this.btnLoading = false
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;

      let sign = 0
      if (row.id) {
        if (row.maxCount != row.syCount) {
          this.$message.warning("仓库" + row.storeName + "不可删除，其中还存放有商品")
          sign = 1
        }
      } else {
        for(let i in this.rows) {
          let row = this.rows[i];
          if (row.maxCount != row.syCount) {
            this.$message.warning("仓库" + row.storeName + "不可删除，其中还存放有商品")
            sign = 1
            break
          }
        }
      }

      if (sign === 0) {
        this.$modal.confirm('是否确认删除仓库管理编号为"' + ids + '"的数据项？').then(function() {
          return delStore(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      }
    },
  }
};
</script>
<style scoped>
.addressTitle{
  width: 90px;
  text-align: right;
  padding-right: 12px;
  line-height: 32px;
  font-size: 14px;
  color: #606266;
  font-weight: bold;
}
/deep/ .el-form-item--medium .el-form-item__content{
  line-height: 32px !important;
}
</style>
